<?php
require_once "../tools/DbTools.php";

$accountCode = $_POST["AccountCode"];
$accountPhone = $_POST["AccountPhone"];
$realName = $_POST["RealName"];
$cardPwd = $_POST["password"];

if (empty($accountCode)||empty($accountPhone)||empty($realName)||empty($cardPwd)){
    exit("请填写完整信息,<a href='createAccount.html'>返回开户页面</a>");
}

DbTools::InitDb();
$sql="select * from accountinfo WHERE AccountCode='$accountCode'";
$result = DbTools::select($sql);
$len = count($result);
if ($len==0){
    $sql="INSERT into accountinfo(AccountCode,AccountPhone,RealName,OpenTime) VALUES('$accountCode','$accountPhone','$realName',NOW())";
    $result = DbTools::noSelect($sql);
    if (!$result["status"]){
        exit("创建账户失败,<a href='createCard.html'>返回开卡页面</a>");
    }
}else{
    $realNameInDb = $result[0]["RealName"];
    if ($realName!=$realNameInDb){
        exit("真实姓名与账户不符，请核对后再试!<a href='createCard.html'>返回开卡页面</a>");
    }
}
$sql="select * from accountinfo WHERE AccountCode='$accountCode' and RealName='$realName'";
$res = DbTools::select($sql);
$accountId = $res[0]["AccountId"];
$cardNo="6";
for ($i=0;$i<12;$i++){
    $rand = rand(0, 9);
    $cardNo.=$rand;
}
//$cardPwd=md5($cardPwd);
$sql="INSERT into bankcard(CardNo,AccountId,CardPwd,CardMoney,CardState,CardTime) VALUES('$cardNo','$accountId','$cardPwd',0,0,NOW())";
$result = DbTools::noSelect($sql);
if (!$result["status"]){
    exit("开卡失败,<a href='createCard.html'>返回开卡页面</a>");
}
echo "开卡成功！<a href='main.html'>返回后台</a>";

DbTools::close();